//
//  ScanCodeView.m
//  YLScaner
//
//  Created by Admin on 2025/9/25.
//

#import "ScanCodeView.h"

@interface ScanCodeView ()

// 扫描线
@property (nonatomic, strong) UIView *scanLine;
// 光晕效果层
@property (nonatomic, strong) CAGradientLayer *glowLayer;
// 动画是否正在运行
@property (nonatomic, assign) BOOL isAnimating;

@end

@implementation ScanCodeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    // 1. 设置扫码框样式
    self.backgroundColor = [UIColor clearColor];
    self.layer.borderColor = [UIColor colorWithRed:74/255.0 green:222/255.0 blue:128/255.0 alpha:1.0].CGColor; // 绿色边框
    self.layer.borderWidth = 1.5f;
    self.layer.cornerRadius = 8.f;
    self.clipsToBounds = NO; // 允许光晕超出边框
    
    // 2. 添加四个角的标记（增强扫码框视觉效果）
    [self addCornerMarkers];
    
    // 3. 创建扫描线
    self.scanLine = [[UIView alloc] init];
    self.scanLine.backgroundColor = [UIColor colorWithRed:74/255.0 green:222/255.0 blue:128/255.0 alpha:1.0]; // 绿色线条
    self.scanLine.layer.cornerRadius = 1.f;
    [self addSubview:self.scanLine];
    
    // 4. 创建顶部光晕效果（使用渐变层实现）
    self.glowLayer = [CAGradientLayer layer];
    self.glowLayer.colors = @[
        (id)[UIColor clearColor].CGColor,
        (id)[UIColor colorWithRed:74/255.0 green:222/255.0 blue:128/255.0 alpha:0.6].CGColor,
        (id)[UIColor colorWithRed:74/255.0 green:222/255.0 blue:128/255.0 alpha:0.2].CGColor
    ];
    self.glowLayer.startPoint = CGPointMake(0.5, 0);
    self.glowLayer.endPoint = CGPointMake(0.5, 1);
    [self.scanLine.layer addSublayer:self.glowLayer];
}

// 添加四个角的标记
- (void)addCornerMarkers {
    CGFloat cornerSize = 20.f;
    CGFloat cornerWidth = 3.f;
    UIColor *cornerColor = [UIColor colorWithRed:74/255.0 green:222/255.0 blue:128/255.0 alpha:1.0];
    
    // 左上角
    UIView *topLeft = [[UIView alloc] init];
    topLeft.backgroundColor = cornerColor;
    topLeft.frame = CGRectMake(0, 0, cornerSize, cornerWidth);
    [self addSubview:topLeft];
    
    UIView *topLeft2 = [[UIView alloc] init];
    topLeft2.backgroundColor = cornerColor;
    topLeft2.frame = CGRectMake(0, 0, cornerWidth, cornerSize);
    [self addSubview:topLeft2];
    
    // 右上角
    UIView *topRight = [[UIView alloc] init];
    topRight.backgroundColor = cornerColor;
    topRight.frame = CGRectMake(self.bounds.size.width - cornerSize, 0, cornerSize, cornerWidth);
    [self addSubview:topRight];
    
    UIView *topRight2 = [[UIView alloc] init];
    topRight2.backgroundColor = cornerColor;
    topRight2.frame = CGRectMake(self.bounds.size.width - cornerWidth, 0, cornerWidth, cornerSize);
    [self addSubview:topRight2];
    
    // 左下角
    UIView *bottomLeft = [[UIView alloc] init];
    bottomLeft.backgroundColor = cornerColor;
    bottomLeft.frame = CGRectMake(0, self.bounds.size.height - cornerWidth, cornerSize, cornerWidth);
    [self addSubview:bottomLeft];
    
    UIView *bottomLeft2 = [[UIView alloc] init];
    bottomLeft2.backgroundColor = cornerColor;
    bottomLeft2.frame = CGRectMake(0, self.bounds.size.height - cornerSize, cornerWidth, cornerSize);
    [self addSubview:bottomLeft2];
    
    // 右下角
    UIView *bottomRight = [[UIView alloc] init];
    bottomRight.backgroundColor = cornerColor;
    bottomRight.frame = CGRectMake(self.bounds.size.width - cornerSize, self.bounds.size.height - cornerWidth, cornerSize, cornerWidth);
    [self addSubview:bottomRight];
    
    UIView *bottomRight2 = [[UIView alloc] init];
    bottomRight2.backgroundColor = cornerColor;
    bottomRight2.frame = CGRectMake(self.bounds.size.width - cornerWidth, self.bounds.size.height - cornerSize, cornerWidth, cornerSize);
    [self addSubview:bottomRight2];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 更新扫描线尺寸（宽度与扫码框一致，高度2px）
    self.scanLine.frame = CGRectMake(0, 0, self.bounds.size.width, 2.f);
    
    // 更新光晕层尺寸（高度为扫描线的5倍，位于扫描线顶部）
    self.glowLayer.frame = CGRectMake(0, -10.f, self.scanLine.bounds.size.width, 12.f);
}

// 开始扫描动画
- (void)startScanAnimation {
    if (self.isAnimating) return;
    self.isAnimating = YES;
    
    // 重置扫描线位置到顶部
    self.scanLine.center = CGPointMake(self.bounds.size.width / 2, 0);
    
    // 执行上下移动动画（2秒一个周期，无限循环）
    [UIView animateWithDuration:2.0
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         // 移动到扫码框底部
                         self.scanLine.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height);
                     }
                     completion:^(BOOL finished) {
                         if (self.isAnimating) {
                             // 动画结束后重新开始（实现无限循环）
                             [self startScanAnimation];
                         }
                     }];
}

// 停止扫描动画
- (void)stopScanAnimation {
    self.isAnimating = NO;
    [self.scanLine.layer removeAllAnimations];
}

@end
